package com.umeox.watch.moto.gallery.ui;


import static com.umeox.watch.moto.gallery.utils.BitmapUtil.bitmapToFile;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.umeox.moto.common.utils.FileUtils;
import com.umeox.watch.moto.gallery.R;
import com.umeox.watch.moto.gallery.adapter.GalleryLayoutManager;
import com.umeox.watch.moto.gallery.adapter.MakeFrameAdapter;
import com.umeox.watch.moto.gallery.adapter.QuickSettingDecoration;
import com.umeox.watch.moto.gallery.adapter.ScaleTransformer;
import com.umeox.watch.moto.gallery.db.AppDataBase;
import com.umeox.watch.moto.gallery.db.model.AlbumInfo;
import com.umeox.watch.moto.gallery.event.OnItemClickListener;
import com.umeox.watch.moto.gallery.utils.BitmapUtil;

import java.io.File;
import java.util.Arrays;
import java.util.List;

public class MakeFrameActivity extends Activity implements OnItemClickListener {

    private MakeFrameAdapter mAdapter;
    private List<Integer> mIntegers;
    private String mImagePath;
    private RecyclerView mSrv;
    public static final Uri CONTENT_URI = Uri.parse("content://com.umeox.watch.moto.galleryProvider/gallery");


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_make_frame);
        mImagePath = getIntent().getStringExtra(BigPhotoActivity.EXTRA_IMAGE_PATH);
        initView();
        initData();

    }


    private void initView() {
        mSrv = findViewById(R.id.srv_make_frame);
        mSrv.setHasFixedSize(true);
        mSrv.addItemDecoration(new QuickSettingDecoration(8, 7));
        mAdapter = new MakeFrameAdapter();
        mAdapter.setImagePath(mImagePath);
        mAdapter.setItemClickListener(this);
        GalleryLayoutManager galleryLayoutManager = new GalleryLayoutManager(GalleryLayoutManager.HORIZONTAL);
        galleryLayoutManager.setItemTransformer(new ScaleTransformer());
        galleryLayoutManager.attach(mSrv);
        mSrv.setAdapter(mAdapter);

    }

    private void initData() {
        mIntegers = Arrays.asList(
                R.drawable.frame_a1,
                R.drawable.frame_a2,
                R.drawable.frame_a3,
                R.drawable.frame_a4,
                R.drawable.frame_b1,
                R.drawable.frame_b2,
                R.drawable.frame_b3,
                R.drawable.frame_b4,
                R.drawable.frame_c1,
                R.drawable.frame_c2,
                R.drawable.frame_c3,
                R.drawable.frame_c4,
                R.drawable.frame_d1,
                R.drawable.frame_d2,
                R.drawable.frame_d3,
                R.drawable.frame_d4
        );
        mAdapter.setDatalist(mIntegers);
    }


    private void insertToDB(File file) {
        AlbumInfo albumInfo = new AlbumInfo();
        albumInfo.imagePath = file.getAbsolutePath();
        albumInfo.imageName = file.getName();
        albumInfo.imageSize = file.length();
        albumInfo.takeTime = file.lastModified();
        AppDataBase.getInstance().albumInfoDao().save(albumInfo);
    }

    @Override
    public void onClick(View view, int position) {

        final int fgId = mIntegers.get(position);
        Bitmap bitmapFg = BitmapFactory.decodeResource(getResources(), fgId);
        Bitmap bitmapBg = BitmapFactory.decodeFile(mImagePath);
        Bitmap bitmap = BitmapUtil.conver(240, 280, bitmapBg);
        File file = bitmapToFile(new File(FileUtils.getAppGalleryCache()), bitmap, bitmapFg);

        insertToDB(file);
        sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
        Intent data = new Intent();
        data.putExtra(BigPhotoActivity.EXTRA_IMAGE_FRAME, file.getAbsolutePath());
        setResult(RESULT_OK, data);

        finish();

    }
}
